package util;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import db.entity.StationStatus;

public class LivePLCThread implements Runnable{

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Thread(new LivePLCThread()).start();
	}
	
	public void run()
	{
		try
		{
			db.EntityWrapper<db.entity.StationStatus> db = new db.EntityWrapper<db.entity.StationStatus>();
			Criterion cri =  Restrictions.eq("id.name", "01");
			db.entity.StationStatus ssItem = db.query(new StationStatus(),100,"id.dataDate","desc",cri).get(0);
			db.commit();
	//		db.entity.StationStatus ssItem = new db.entity.StationStatus();
	//		db.entity.StationStatusPK ssItemPK = new db.entity.StationStatusPK();
	//		ssItem.setId(ssItemPK);
			float presL, presR, vacL, vacR, temp;
			presL = ssItem.getPressureLeft();
			presR = ssItem.getPressureRight();
			vacL = ssItem.getVacuumLeft();
			vacR = ssItem.getVacuumRight();
			temp = ssItem.getTemperature();

			long time = ssItem.getId().getDataDate().getTime();
			String sName = ssItem.getId().getName();
			
			for(int i=0; i<15; i++)
			{
				db.entity.StationStatus ssItem2 = new db.entity.StationStatus();
				db.entity.StationStatusPK ssItem2PK = new db.entity.StationStatusPK();
				ssItem2.setId(ssItem2PK);
				
				switch(i%4)
				{
				case 1:
					presL++; presR+=2; vacL++; vacR+=3; temp++;
					break;
				case 2:
					presL+=2; presR++; vacL+=4; vacR++; temp+=5;
					break;
				case 3:
					presL-=3; presR-=2; vacL-=5; vacR-=4; temp-=3;
					break;
				case 0:
					presL--; presR++; vacL--; vacR++; temp--;
					break;
				}

				time += 3000;
				
				ssItem2PK.setName(sName);				
				ssItem2.getId().setDataDate(new java.util.Date(time));
				ssItem2.setPressureLeft(presL);
				ssItem2.setPressureRight(presR);
				ssItem2.setVacuumLeft(vacL);
				ssItem2.setVacuumRight(vacR);
				ssItem2.setTemperature(temp);
				
				db = new db.EntityWrapper<db.entity.StationStatus>();
				db.merge(ssItem2);
				db.commit();
				System.out.println("Insert " + ssItem.getId().getDataDate());
				Thread.sleep(3000);
			}
			
		} catch( Exception e) {
			e.printStackTrace();
		}
		
	}

}
